using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	public Transform player;
	public float speed;
	public int chapter;
	public float dangerRange = 5f;
	public float killingRange = 2f;

	AudioMovement audioScript;
	PlayerMovement playerScript;


	// Use this for initialization
	void Start () {
		audioScript = GameObject.FindObjectOfType(typeof(AudioMovement)) as AudioMovement;
		playerScript = GameObject.FindObjectOfType(typeof(PlayerMovement)) as PlayerMovement;
	}
	
	// Update is called once per frame
	void Update () {

		if (audioScript.GetActiveCheckpointID() != chapter)
			return;

		Vector3 dist = (player.position - transform.position);
		transform.Translate(dist.normalized * speed * Time.deltaTime);

		if (dist.magnitude < dangerRange)
		{
			if (!playerScript.isInDanger())
			{
				audioScript.SetActiveLines("danger");
				playerScript.EnterDanger();
			}
		}
		else if (playerScript.isInDanger())
			playerScript.ExitDanger();

		if (dist.magnitude < killingRange)
			playerScript.Kill();
	}

	void PlaySound(AudioSource src)
	{
		if (!src.isPlaying)
			src.Play();
	}
	
}
